package com.zhang.generateexcel.util;

import cn.hutool.core.util.BooleanUtil;
import lombok.var;

import java.util.regex.Pattern;

public class IntegerUtil {

    public static final Integer invalidValue = -1;

    public static Integer parse(Object value) {
        try {
            return Integer.valueOf(value.toString());
        } catch (Exception ex) {
            return null;
        }
    }

    public static Integer convert(Object value) {
        return parse(value);
    }

    public static int parseValue(Object value) {
        try {
            return Integer.parseInt(value.toString());
        } catch (Exception ex) {
            return 0;
        }
    }

    public static int value(Object value) {
        return parseValue(value);
    }

    public static int value(Boolean value) {
        return BooleanUtil.isTrue(value) ? 1 : 0;
    }

    public static boolean equals(Object item, Object... values) {
        return ObjectUtil.equals(item, values);
    }

    public static boolean notEquals(Integer value, Integer... values) {
        return !equals(value, values);
    }

    public static boolean greaterThan(Integer a, Integer b) {
        return value(a) > value(b);
    }

    public static boolean gt(Integer a, Integer b) {
        return greaterThan(a, b);
    }

    public static boolean gt(Object a, Object b) {
        return greaterThan(parse(a), parse(b));
    }

    public static boolean lessThan(Integer a, Integer b) {
        return value(a) < value(b);
    }

    public static boolean lt(Integer a, Integer b) {
        return lessThan(a, b);
    }

    public static boolean le(Integer a, Integer b) {
        return value(a) <= value(b);
    }

    public static boolean ge(Integer a, Integer b) {
        return value(a) >= value(b);
    }

    /**
     * 反向布尔 0是 1 否
     */
    public static int reverseBoolean(boolean result) {
        return result ? 0 : 1;
    }

    /**
     * 反向布尔 0是 1 否
     */
    public static int reverseInt(Integer result) {
        return equals(result, 1) ? 0 : 1;
    }

    private static final Pattern pattern = Pattern.compile("[0-9]*");

    /**
     * 是否是整形
     *
     * @param obj
     * @return
     */
    public static boolean isNumber(Object obj) {
        var text = obj.toString();
        if (pattern.matcher(text).matches()) {
            return true;
        }
        return false;
    }

    public static int add(Object... value) {
        int total = 0;
        for (Object v : value) {
            total += parseValue(v);
        }
        return total;
    }
}
